//
//  ConeBaseViewController.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/2/5.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "ConeBaseViewController.h"
#import "TYCyclePagerView.h"
#import "TYCyclePagerViewCell.h"
#import "TGSementBarVC.h"
#import "ConBaseIntroVc.h"
#import "ConSepcialVc.h"
#import "BaseSingleSureView.h"
#import "CoverView.h"
#import "BaseShareTool.h"
#import "SZLAccountHelper.h"
#import "NewHomeNetwork.h"
#import "ClubModel.h"
#import "MBProgressHUD+MJ.h"
#import "SingleCreateModel.h"
#import "ConBaseSinglePayVc.h"

#define RoolH 0.33*ScreenH
@interface ConeBaseViewController ()<TYCyclePagerViewDataSource,TYCyclePagerViewDelegate>

@property (nonatomic,strong) TYCyclePagerView *pagerView;
@property (nonatomic,strong) NSMutableArray *rImgs;
@property (nonatomic,strong) TGSementBarVC *segmentBarVC;
@property (nonatomic,weak ) ConBaseIntroVc *intoVc;
@property (nonatomic,weak) ConSepcialVc *specialVc;
@property (nonatomic,weak) UILabel *numLab;
@property (nonatomic,weak) BaseSingleSureView *singleView;

@property (nonatomic,strong) ClubModel *model;//基地model
@property (nonatomic,strong) SpecialModel *sModel;//选中的特色服务
@property (nonatomic,strong) SingleCreateModel *singleModel;//选中的特色服务

@end

@implementation ConeBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setRoolView];
    [self setNavi];
    [self setUpView];
    [self getClubDetail];
}
- (void)setNavi
{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, SafeAreaTopHeight-35-10, 35, 35)];
    backBtn.backgroundColor = RGBA(0, 0, 0, 0.7);
    [backBtn setImage:GetImage(@"返回small") forState:UIControlStateNormal];
    backBtn.layer.cornerRadius = backBtn.width*0.5;
    backBtn.clipsToBounds = YES;
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW-10-35, SafeAreaTopHeight-35-10, 35, 35)];
    rightBtn.backgroundColor = RGBA(0, 0, 0, 0.7);
    [rightBtn setImage:GetImage(@"点点白") forState:UIControlStateNormal];
    rightBtn.layer.cornerRadius = backBtn.width*0.5;
    rightBtn.clipsToBounds = YES;
    [rightBtn addTarget:self action:@selector(moreClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightBtn];
}
- (void)backClick
{
    if (self.specialVc.isPush) {
        [self.specialVc setPushHidden];
    }else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
//点击点点点
- (void)moreClick
{
    CoverView *cover = [[CoverView alloc]initWithFrame:[UIScreen mainScreen].bounds Titles:@[@"赚赏金"] backStart:57];
    cover.btnBlock = ^(NSInteger index){
        [BaseShareTool baseShareWithImg:GetImage(@"lunbo1")];
    };
    [cover showSelectView];
}
- (void)setRoolView
{
    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, RoolH)];
    pagerView.isInfiniteLoop = YES;
    pagerView.autoScrollInterval = 3.0;
    pagerView.dataSource = self;
    pagerView.delegate = self;
    // registerClass or registerNib
    [pagerView registerClass:[TYCyclePagerViewCell class] forCellWithReuseIdentifier:NSStringFromClass([TYCyclePagerViewCell class])];
    self.pagerView = pagerView;
    [self.view addSubview:pagerView];
    
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenW-40, RoolH-50, 20, 20)];
    img.image = GetImage(@"轮播图");
    [self.view addSubview:img];
    UILabel *numLab = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW-75, RoolH-50, 30, 20)];
    numLab.textColor = [UIColor whiteColor];
    numLab.font = Font(14);
    numLab.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:numLab];
    self.numLab = numLab;
}
- (void)setUpView
{
    UIView *segView = [[UIView alloc]initWithFrame:CGRectMake(0, RoolH-20, ScreenW, 80)];
    segView.backgroundColor = [UIColor whiteColor];
    segView.layer.cornerRadius = 20;
    segView.clipsToBounds = YES;
    [self.view addSubview:segView];
    
    self.segmentBarVC.segmentBar.frame = CGRectMake(20, 15, ScreenW-40, 35);
    [segView addSubview:self.segmentBarVC.segmentBar];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 51, ScreenW, 1)];
    lineView.backgroundColor = RGBColorOne(200);
    [segView addSubview:lineView];
    
    //设置segmentBarVC大小
    self.segmentBarVC.view.frame = CGRectMake(0, CGRectGetMaxY(segView.frame)-28, ScreenW, ScreenH-CGRectGetMaxY(segView.frame)+28);
    [self addChildViewController:self.segmentBarVC];
    [self.view addSubview:self.segmentBarVC.view];
    
    NSArray *items = @[@"基地介绍",@"特色服务"];
    if (self.activityId) {
        items = @[@"基地介绍",@"预订服务"];
    }else {
        if (self.mapModel.clubType == 2) {
            items = @[@"基地介绍",@"特色服务"];
        }else if (self.mapModel.clubType == 3) {
            items = @[@"会所介绍",@"免费喝茶"];
        }
    }
    NSMutableArray* childVCs = [NSMutableArray array];
    ConBaseIntroVc *vc = [[ConBaseIntroVc alloc]init];
    self.intoVc = vc;
    vc.roolH = RoolH;
    [childVCs addObject:vc];
    
    ConSepcialVc *sVc = [[ConSepcialVc alloc]init];
    sVc.roolH = RoolH;
    self.specialVc = sVc;
    sVc.activityId = self.activityId;//从约么进去的要传活动id
    sVc.buyBlock = ^(SpecialModel *model) {
        if (model.maxReserve < model.count) {
            [MBProgressHUD showError:@"报名人数已满"];
            return ;
        }
        //计算总价格
        float price = model.price*model.count;
        model.sumPrice = price;
        self.sModel = model;
        [self buyAction];
    };
    [childVCs addObject:sVc];
    [self.segmentBarVC setupWithItems:items childVCs:childVCs];
    [self.segmentBarVC.segmentBar updateViewWithConfig:^(TGSegmentConfig *config) {
         config.selectedColor([UIColor blackColor])
        .normalColor(RGBColorOne(80))
        .selectedFont([UIFont systemFontOfSize:18])
        .normalFont([UIFont systemFontOfSize:18])
        .indicateExtraW(20)
        .indicateH(2)
        .indicateColor(RGBColor(234, 111, 90))
        .showMore(NO)//是否显示更多面板
        .circleScroll(NO)//是否循环滚动，第0个再向前，那么到最后一个;最后一个向后，那么到第0个
        .margin(18)
        .barBGColor([UIColor whiteColor]);
    }];
}
//购买弹出
- (void)buyAction
{
    self.singleView.model = self.sModel;
    [self.view addSubview:self.singleView];
    [self.singleView animatedShow];
}
- (BaseSingleSureView *)singleView
{
    if (!_singleView) {
        _singleView = [BaseSingleSureView singleSureView];
        _singleView.frame = self.view.bounds;
        _singleView.backgroundColor = RGBA(0, 0, 0, 0.3);
        _singleView.payBlock = ^(SingleCreateModel *singleModel) {
            ConBaseSinglePayVc *vc = [[ConBaseSinglePayVc alloc] init];
            vc.specialModel = self.sModel;
            vc.clubModel = self.model;
            vc.activityId = self.activityId;
            [self.navigationController pushViewController:vc animated:YES];
        };
    }
    return _singleView;
}
#pragma mark - TYCyclePagerViewDataSource

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.rImgs.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    TYCyclePagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([TYCyclePagerViewCell class]) forIndex:index];
    cell.radio = 0;
    cell.imagePlace = self.rImgs[index];
    return cell;
}
- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex
{
    self.numLab.text = [NSString stringWithFormat:@"%zd/%zd",toIndex+1,self.rImgs.count];
}
- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(ScreenW, RoolH);
    layout.layoutType = TYCyclePagerTransformLayoutNormal;
//    layout.itemSpacing = 0;
//    layout.itemHorizontalCenter = YES;
    return layout;
}
#pragma mark-------------------基地详情----------------
- (void)getClubDetail
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"clubId"] = self.mapModel.ID;
    if ([SZLAccountHelper isAlreadyLogin]) {
        parame[@"token"] = [SZLAccountHelper shareInstance].token;
    }
    [self showClearBgWaiter];
    [NewHomeNetwork getClubDetailWithParame:parame response:^(BOOL success, NSString *msg, ClubModel *model) {
        [self hideWaiter];
        if (success) {
            self.model = model;
            self.intoVc.model = model;
            self.specialVc.model = model;
            self.specialVc.mapModel = self.mapModel;
            [self.rImgs addObjectsFromArray:[model.backgroundImg componentsSeparatedByString:@"#"]];
            if (self.rImgs.count <= 1) {
                self.pagerView.isInfiniteLoop = NO;
            }
            [self.pagerView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}

- (TGSementBarVC *)segmentBarVC
{
    if (!_segmentBarVC) {
        _segmentBarVC = [[TGSementBarVC alloc] init];
    }
    return _segmentBarVC;
}
- (NSMutableArray *)rImgs
{
    if (!_rImgs) {
        _rImgs = [NSMutableArray array];
    }
    return _rImgs;
}

@end
